import 'package:aihu_getx_app/app/interceptors/login_intercept_chain.dart';
import 'package:aihu_getx_app/app/modules/commons/model/login_res_model.dart';
import 'package:aihu_getx_app/app/modules/user_login/models/user_login_form_model.dart';
import 'package:aihu_getx_app/providers/http_provider.dart';
import 'package:aihu_getx_app/utils/cache_keys.dart';
import 'package:dio/dio.dart';
import 'package:sp_util/sp_util.dart';

class UserLoginProvider {
  static Future<LoginResModel> login(UserLoginFormModel form) async {
    var response = await Http.post(
      "/member/auth/login",
      data: form.toJson(),
      options: Options(
        headers: {
          "isLogin": true,
        },
      ),
      fromJsonT: (json) {
        return LoginResModel.fromJson(json);
      },
    );
    SpUtil.putObject(CacheKeys.userLoginInfo, response)?.then(
      (value) => Http.refreshHeaderToken(),
    );
    return Future(() => response);
  }

  static Future<bool> logout() async {
    var response = await Http.post(
      "/member/auth/logout",
      options: Options(
        headers: {
          "isLogout": true,
        },
      ),
      fromJsonT: (json) {
        return json;
      },
    );
    var data = response;
    LoginInterceptChain.logout();
    Http.refreshHeaderToken();
    return Future(() => data);
  }
}
